package com.ttms.web.manage;

import com.ttms.dao.UserDao;
import com.ttms.dao.impl.UserDaoImpl;
import com.ttms.pojo.Page;
import com.ttms.pojo.Play;
import com.ttms.pojo.Schedule;
import com.ttms.pojo.User;
import com.ttms.service.UserService;
import com.ttms.service.impl.UserServiceImpl;
import com.ttms.utils.WebUtils;
import com.ttms.web.common.BaseServlet;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UsermanageServlet extends BaseServlet {


    private UserService userService = new UserServiceImpl();
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<User> page = userService.page(pageNo,pageSize);
        page.setUrl("/ttms/manage/usermanageServlet?action=page");
        req.setAttribute("page",page);
        req.getRequestDispatcher("/front-end/views/manage/usermanage.jsp").forward(req,resp);
    }

    protected void resetPassword(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        userService.resetPassword("000000", id);
        resp.sendRedirect("/ttms/manage/usermanageServlet?action=page");

    }
    protected void rename(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        String phone = req.getParameter("phone");
        String username = req.getParameter("username");
        userService.rename(phone,username, id);
        resp.sendRedirect("/ttms/manage/usermanageServlet?action=admin");

    }
    protected void editPassword(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        String password = req.getParameter("password");
            if (password.equals(userService.queryUserById(1).getPassword())){
                String newpassword = req.getParameter("newpassword");
                userService.resetPassword(newpassword, 1);
            }
        req.getRequestDispatcher("/login.jsp").forward(req,resp);

    }


    protected void admin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = userService.queryUserById(1);
        req.setAttribute("user", user);
        req.getRequestDispatcher("/front-end/views/manage/space.jsp").forward(req,resp);
    }

    protected void resetHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (ServletFileUpload.isMultipartContent(req)) {
            System.out.println("1");
            FileItemFactory fileItemFactory = new DiskFileItemFactory();
            ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
            try {
                List<FileItem> list = servletFileUpload.parseRequest(req);
                for (FileItem fileItem : list) {
                    if (!fileItem.isFormField()) {
                        String fileName = fileItem.getName();
                        if (!(fileName.equals(null)&&fileName.equals(""))){
                            System.out.println(fileName);
                            String sname = fileName.substring(fileName.lastIndexOf("."));
                            SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyyMMddHHmmssSSS");
                            String timeStamp=simpleDateFormat.format(new Date());
                            String newName ="user"+timeStamp+sname;
                            String path = "F:\\code\\ttms\\src\\main\\webapp\\front-end\\img" + newName;
                            System.out.println(path);
                            fileItem.write(new File(path));
                            String head_path =  "front-end/img/"+newName;
                            userService.resetHead(head_path);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        resp.sendRedirect("/ttms/manage/usermanageServlet?action=admin");
    }
}
